/*
 * lex_common.c
 * This file is part of gURL
 *
 * Copyright (C) 2014 - Aleksey Konovalov
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Library General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "lex_common.h"

#include <string.h>

static GtkTextTag* get_tag(GtkTextBuffer* tb, const gchar* name, const gchar* first_prop_name, va_list vl) {
    GtkTextTagTable* tag_table = gtk_text_buffer_get_tag_table(tb);
    GtkTextTag* tag = gtk_text_tag_table_lookup(tag_table, name);
    if(!tag) {
        tag = gtk_text_tag_new(name);
        g_object_set_valist(G_OBJECT(tag), first_prop_name, vl);
        gtk_text_tag_table_add(tag_table, tag);
    }
    return tag;
}

static void get_iter_by_byte_offset(GtkTextIter* iter, struct ParseData* pd,
                                    const gchar* pos) {
    size_t line_id = 0;
    while(line_id + 1 < pd->lines_index->len &&
          g_array_index(pd->lines_index, gchar*, line_id + 1) <= pos)
        ++line_id;

    const size_t offset = pos - g_array_index(pd->lines_index, gchar*, line_id);
    gtk_text_buffer_get_iter_at_line_index(pd->tb, iter, line_id, offset);
}

void mark_text(struct ParseData* pd, const gchar* text, size_t length,
               const gchar* tagname, const gchar* first_tag_prop_name, ...) {
    if(!first_tag_prop_name || length == 0) return;

    va_list vl;
    va_start(vl, first_tag_prop_name);
    GtkTextTag* tag = get_tag(pd->tb, tagname, first_tag_prop_name, vl);
    va_end(vl);

    if(!pd->prettyprint) {
        GtkTextIter begin = {0}, end = {0};
        get_iter_by_byte_offset(&begin, pd, text);
        get_iter_by_byte_offset(&end, pd, text + length);
        gtk_text_buffer_apply_tag(pd->tb, tag, &begin, &end);
    } else {
        GtkTextIter it = {0};
        if(pd->indentstr->len > 0) {
            gtk_text_buffer_get_end_iter(pd->tb, &it);
            gtk_text_buffer_insert(pd->tb, &it, pd->indentstr->str,
                                   pd->indentstr->len);
            g_string_truncate(pd->indentstr, 0);
        }

        gtk_text_buffer_get_end_iter(pd->tb, &it);
        gtk_text_buffer_insert_with_tags(pd->tb, &it, text, length, tag, NULL);
    }
}

void mark_eol(struct ParseData* pd) {
    if(pd->prettyprint) {
        const int is = pd->indentsize >= 0 ? pd->indentsize : 0;
        g_string_append_c(pd->indentstr, '\n');
        for(int i = 0; i < is; ++i)
            g_string_append_c(pd->indentstr, '\t');
    }
}

void reset_indent(struct ParseData* pd) {
    if(pd->indentstr)
        g_string_truncate(pd->indentstr, 0);
}

void inc_indent(struct ParseData* pd) {
    ++pd->indentsize;
}

void dec_indent(struct ParseData* pd) {
    --pd->indentsize;
}
